# (C)2019 千叶集团 All rights reserved.
# 代码编写：真心
# 个人主页：zhenxin.xyz
# 多合一编写：https://github.com/cjybyjk/magisk-allinone-module-template

# 配置
AUTOMOUNT=true
POSTFSDATA=false
LATESTARTSERVICE=false

source $INSTALLER/common/var.sh
source $INSTALLER/common/util_funcs.sh

# 在这里设置你想要在模块安装过程中显示的信息
ui_print "  Redmi 6 Pro 状态栏增强模块"
ui_print "  欢迎加入交流群：691802087"
ui_print "  作者：真心"
ui_print "  B站/酷安/微博：@QG真心"
ui_print "  微信公众号：真心极客"
ui_print "  个人主页：www.zhenxin.xyz"
ui_print "******************************"
ui_print "  设备型号：$var_model ($var_device)"
ui_print "  系统版本：MIUI 10 $var_version"
ui_print "  支持版本：MIUI 10 $var_miuiver"
ui_print "  安卓版本：$var_android (API$var_androidsdk)"
ui_print "******************************"
ui_print "  请勿和其他状态栏或系统UI(SystemUI)相关模块一起使用"
ui_print "******************************"

if [ ! "$var_version" = "$var_miuiver" ];then
  ui_print "  不支持您的系统版本，请升级系统"
  ui_print "******************************"
  exit 1
fi

if [ -d "/data/adb/modules/sakura_status_bar_plus" ];then
  ui_print "  检测到旧ID模块存在，已删除"
  ui_print "******************************"
  rm -rf /data/adb/modules/sakura_status_bar_plus
  if [ -d "/data/adb/modules_update/sakura_status_bar_plus" ];then
      rm -rf /data/adb/modules_update/sakura_status_bar_plus
  fi
fi

if [ -d "/data/adb/modules/Z-Sakura" ];then
  ui_print "  检测到您上一版本的模块未删除"
  ui_print "  已为您删除，请重启后重新刷入"
  ui_print "******************************"
  touch /data/adb/modules/Z-Sakura/remove
  rm -rf /data/adb/modules_update/Z-Sakura
  rm -f /data/system/package_cache/1/*
  exit
fi

initmods()
{
  mod_name=""
  mod_install_info=""
  mod_select_yes_text=""
  mod_select_yes_desc=""
  mod_select_no_text=""
  mod_select_no_desc=""
  mod_require_device=""
  mod_require_version=""
  INSTALLED_FUNC="`trim $INSTALLED_FUNC`"
  MOD_SKIP_INSTALL=false
  cd $INSTALLER/common/mods
}

# 准备进行音量键安装
# Keycheck binary by someone755 @Github, idea for code below by Zappo @xda-developers
KEYCHECK=$INSTALLER/common/keycheck
chmod 755 $KEYCHECK
#zip
MYZIP=$INSTALLER/common/zip
chmod 755 $MYZIP

keytest() {
  ui_print "- 音量键测试 -"
  ui_print "   请按下 [音量+] 键："
  ui_print "   无反应或传统模式无法正确安装时，请触摸一下屏幕后继续"
  (/system/bin/getevent -lc 1 2>&1 | /system/bin/grep VOLUME | /system/bin/grep " DOWN" > $INSTALLER/events) || return 1
  return 0
}

chooseport() {
  #note from chainfire @xda-developers: getevent behaves weird when piped, and busybox grep likes that even less than toolbox/toybox grep
  while (true); do
    /system/bin/getevent -lc 1 2>&1 | /system/bin/grep VOLUME | /system/bin/grep " DOWN" > $INSTALLER/events
    if (`cat $INSTALLER/events 2>/dev/null | /system/bin/grep VOLUME >/dev/null`); then
      break
    fi
  done
  if (`cat $INSTALLER/events 2>/dev/null | /system/bin/grep VOLUMEUP >/dev/null`); then
    return 0
  else
    return 1
  fi
}

chooseportold() {
  # Calling it first time detects previous input. Calling it second time will do what we want
  $KEYCHECK
  $KEYCHECK
  SEL=$?
  if [ "$1" == "UP" ]; then
    UP=$SEL
  elif [ "$1" == "DOWN" ]; then
    DOWN=$SEL
  elif [ $SEL -eq $UP ]; then
    return 0
  elif [ $SEL -eq $DOWN ]; then
    return 1
  else
    abort "   未检测到音量键!"
  fi
}

# 测试音量键
if keytest; then
	VOLKEY_FUNC=chooseport
	ui_print "******************************"
else
	VOLKEY_FUNC=chooseportold
	ui_print "******************************"
	ui_print "- 检测到遗留设备！使用旧的 keycheck 方案 -"
	ui_print "- 进行音量键录入 -"
	ui_print "   录入：请按下 [音量+] 键："
	$VOLKEY_FUNC "UP"
	ui_print "   已录入 [音量+] 键。"
	ui_print "   录入：请按下 [音量-] 键："
	$VOLKEY_FUNC "DOWN"
	ui_print "   已录入 [音量-] 键。"
	ui_print "******************************"
fi

ui_print "  检测到您已安装以下模块："
cd /data/adb/modules/
for module_id in $(ls)
do
  let id++
  module_name=$(cat $module_id/module.prop | grep 'name')
  module_author=$(cat $module_id/module.prop | grep 'author')
  module_description=$(cat $module_id/module.prop | grep 'description')
  ui_print "  $id.$module_id"
  ui_print "    名称：${module_name:5}"
  ui_print "    作者：${module_author:7}"
  ui_print "    简介：${module_description:12}"
done
ui_print "******************************"
ui_print "  请检查本模块与以上模块是否冲突"
ui_print "******************************"
ui_print "  是否继续安装本模块？"
ui_print "  [音量+] 继续安装"
ui_print "  [音量-] 退出安装"
if ! $VOLKEY_FUNC ; then
  exit 1
fi
ui_print "  模块将内置一个APP，暂时没有功能。"
ui_print "******************************"
ui_print "  是否同意安装APP？"
  ui_print "  [音量+] 同意安装"
  ui_print "  [音量-] 拒绝安装"
if $VOLKEY_FUNC ; then
  mkdir -p $MODPATH/system
  mkdir -p $MODPATH/system/app
  cp -r $INSTALLER/common/app/* $MODPATH/system/app
fi
# 替换文件夹列表
REPLACE=""

# 已安装模块
MODS_SELECTED_YES=""
MODS_SELECTED_NO=""

# 加载可用模块
initmods
for MOD in $(ls)
do
  if [ -f $MOD/mod_info.sh ]; then
    MOD_FILES_DIR="$INSTALLER/common/mods/$MOD/files"
    source $MOD/mod_info.sh
    ui_print "  [$mod_name]安装"
    if $MOD_SKIP_INSTALL ; then
      MOD_SKIP_INSTALL=false
      ui_print "  跳过[$mod_name]安装"
      initmods
      continue
    fi
    if [ "`echo $var_device | egrep $mod_require_device`" = "" ]; then
      ui_print "   [$mod_name]不支持您的设备。"
    elif [ "`echo $var_version | egrep $mod_require_version`" = "" ]; then
      ui_print "   [$mod_name]不支持您的系统版本。"
    else
      ui_print "    说明：$mod_install_info"
      ui_print "   [音量+]：$mod_select_yes_text"
      ui_print "   [音量-]：$mod_select_no_text"
      if $VOLKEY_FUNC; then
        ui_print "   已选择$mod_select_yes_text。"
        mod_install_yes
        run_result=$?
        if [ $run_result -eq 0 ]; then
          MODS_SELECTED_YES="$MODS_SELECTED_YES ($MOD)"
          INSTALLED_FUNC="$mod_select_yes_desc $INSTALLED_FUNC"
        else
          ui_print "   失败。错误: $run_result"
        fi
      else
        ui_print "   已选择$mod_select_no_text。"
        mod_install_no
        run_result=$?
        if [ $run_result -eq 0 ]; then
          MODS_SELECTED_NO="$MODS_SELECTED_NO ($MOD)"
          INSTALLED_FUNC="$mod_select_no_desc $INSTALLED_FUNC"
        else
          ui_print "   失败。错误: $run_result"
        fi
      fi
    fi
  fi
  initmods
done

if [ -z "$INSTALLED_FUNC" ]; then
  ui_print "未安装任何功能 即将退出安装..."
  rm -rf $TMPDIR
  exit 1
fi

sed -i '/^\s*$/d' $INSTALLER/module.prop
echo "$INSTALLED_FUNC" >> $INSTALLER/module.prop
sed -i '/^\s*$/d' $INSTALLER/module.prop
